/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.menu;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author johan
 */
@ManagedBean(name="createMenuName")
@SessionScoped

public class CreateDinnerMenuName implements Serializable{
    
     private int i;
     private String sql;
    
     @Resource(name="jdbc/antonsDatasource")
         
     private DataSource dataSource;
     
     private String menuName;

    public void setSql(String sql) {
        this.sql = sql;
    }
     
    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }    
      
     public  String add() throws SQLException {   
        if(dataSource==null)
            throw new SQLException("Can't get data source");// throws exception i database not available
 
        //database connection
	Connection con = dataSource.getConnection();
                 
	if(con==null)
            throw new SQLException("Can't get database connection");
        try (PreparedStatement ps = con.prepareStatement(
             sql)){
            
            ps.setString( 1, menuName );
             
            i = ps.executeUpdate();   
         }      
         
        
         con.close();  
         this.menuName=null;
       
         return "/admin/dinnermenu/test?faces-redirect=true";     
    } 
    
    
}
